package com.youtellv1.controller;

import com.youtellv1.config.ResultData;
import com.youtellv1.entity.TeacherSign;
import com.youtellv1.entity.Teachers;
import com.youtellv1.service.TeacherSignService;
import com.youtellv1.service.TeachersService;
import com.youtellv1.utils.SignDateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/manager/teacherSign")
public class TeacherSignController {

    @Autowired
    private TeacherSignService teacherSignService;

    @Autowired
    private TeachersService teachersService;

    @GetMapping("/info")
    public ResultData<Map<String, Object>> getTeacherSignInfo(@RequestParam("tid") Integer tid) {
        Map<String, Object> result = new HashMap<>();

        Teachers teacher = teachersService.findNameByTid(tid);;
        result.put("tname", teacher.getTname());

        boolean signedToday = teacherSignService.isTodaySigned(tid);
        result.put("flag", signedToday);

        List<Date> allSignDates = teacherSignService.findAllSignDates(tid);
        result.put("timelist", allSignDates);

        List<Date> monthSignDates = teacherSignService.findThisMonthSignDates(tid);
        result.put("mlist", monthSignDates);

        int continuousDays = SignDateUtils.calculateContinuousWorkdaySignIns(allSignDates);
        result.put("continuous", continuousDays);

        return new ResultData<>(result);
    }

    @GetMapping("/checksign_t")
    public String checkTeacherSign(@RequestParam("tid") Integer tid,
                                   @RequestParam("cont") String cont) {
        try {
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date signDate = format.parse(cont);

            boolean exists = teacherSignService.hasSignedOnDate(tid, signDate);
            return exists ? "1" : "";
        } catch (Exception e) {
            return "error";
        }
    }

    @GetMapping("/rep_sign_t")
    public ResultData<Void> repairSign(@RequestParam("tid") Integer tid,
                                       @RequestParam("cont") String cont) {
        try {
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date signDate = format.parse(cont);

            TeacherSign sign = new TeacherSign();
            sign.setTid(tid);
            sign.setSdate(signDate);

            teacherSignService.insertSignRecord(sign);
            return new ResultData<>();
        } catch (Exception e) {
            return new ResultData<>(500, "补签失败：" + e.getMessage(), null);
        }
    }

    @PostMapping("/sign_in")
    public ResultData<Void> signInToday(@RequestParam("tid") Integer tid) {
        try {
            if (teacherSignService.hasSignedToday(tid)) {
                return new ResultData<>(400, "今日已签到，无需重复签到", null);
            }
            TeacherSign sign = new TeacherSign();
            sign.setTid(tid);
            sign.setSdate(new Date());
            teacherSignService.insertTodaySign(sign);
            return new ResultData<>();
        } catch (Exception e) {
            return new ResultData<>(500, "签到失败：" + e.getMessage(), null);
        }
    }
}
